package net.zhnb.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Random;

import org.apache.commons.codec.binary.Base64;

public class ProjectUtil {
	
	/**
	 * base64编码
	 * @param origin
	 * @return
	 */
    public static String encodeBase64(String origin) {
        if (origin == null) {
            return "";
        }
        return new String(Base64.encodeBase64(origin.getBytes()));
    }
    
	/**
	 * base64解码
	 * @param dest
	 * @return
	 */
    public static String decodeBase64(String dest) {
        if (dest == null) {
            return "";
        }
        return new String(Base64.decodeBase64(dest.getBytes()));
    }
    /**
     * 判断字符是不是数字
     * @param str
     * @return
     */
	public static boolean isNum(String str){
		return str.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
	}
	
	/**
	 * 概率与命中
	 * 传入概率,得出命中与否
	 */
	public static boolean probabilityHit(int gaiLv){
		return new Random().nextInt(100)>=(100-gaiLv)?true:false;
	}
	
	/**
	 * 得到当前时间getCurrentFormatStr
	 * 
	 * @param strformat
	 * @return String
	 * 
	 */
	public static String getCurrentFormatStr(String strformat) {
		Calendar now = Calendar.getInstance();
		SimpleDateFormat formatter = new SimpleDateFormat(strformat);
		String sr = formatter.format(now.getTime());
		return sr;
	}
	
}
